/**
 * 
 */
package be.bejug.scrumadmin.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import be.bejug.scrumadmin.domain.enums.Priority;
import be.bejug.scrumadmin.domain.enums.Status;

/**
 * @author Glenn Dejaeger
 * 
 */
@Entity
@Table(name = "TASKS", schema = "SCRUMADMIN")
public class Task extends DomainObject implements Serializable,
		Comparable<Task> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4764115177762785594L;

	@Column(name = "NAME", nullable = false)
	private String name;

	@Column(name = "DESCRIPTION", nullable = false)
	private String description;

	@Column(name = "ESTIMATION")
	private Double estimation;

	@Column(name = "WORKLEFT")
	private Double workLeft;

	@OneToOne
	@JoinColumn(name = "ASSIGNEDTO")
	private User assignedTo;

	@OneToOne
	@JoinColumn(name = "CREATEDBY", nullable = false)
	private User createdBy;

	@Column(name = "PRIORITY", nullable = true)
	private Priority priority;

	@ManyToOne
	@JoinColumn(name = "STORY_ID", nullable = false)
	private Story story;

	@Column(name = "STATUS", nullable = false)
	private Status status;

	/**
	 * @param name
	 * @param description
	 * @param estimation
	 * @param createdBy
	 * @param priority
	 */
	public Task(final int id, final String name, final String description,
			final Double estimation, final User createdBy,
			final Priority priority) {
		this(id, name, description, estimation, createdBy, priority,
				Status.TODO);
	}

	/**
	 * @param name
	 * @param description
	 * @param estimation
	 * @param createdBy
	 * @param priority
	 * @param status
	 */
	public Task(final int id, final String name, final String description,
			final Double estimation, final User createdBy,
			final Priority priority, final Status status) {
		super();
		this.id = id;
		this.name = name;
		this.description = description;
		this.estimation = estimation;
		this.createdBy = createdBy;
		this.priority = priority;
		this.status = status;
		this.workLeft = estimation;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(final String name) {
		this.name = name;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(final String description) {
		this.description = description;
	}

	/**
	 * @return the estimation
	 */
	public Double getEstimation() {
		return estimation;
	}

	/**
	 * @param estimation
	 *            the estimation to set
	 */
	public void setEstimation(final Double estimation) {
		this.estimation = estimation;
	}

	/**
	 * @return the workLeft
	 */
	public Double getWorkLeft() {
		return workLeft;
	}

	/**
	 * @param workLeft
	 *            the workLeft to set
	 */
	public void setWorkLeft(final Double workLeft) {
		this.workLeft = workLeft;
	}

	/**
	 * @return the assignedTo
	 */
	public User getAssignedTo() {
		return assignedTo;
	}

	/**
	 * @param assignedTo
	 *            the assignedTo to set
	 */
	public void setAssignedTo(final User assignedTo) {
		this.assignedTo = assignedTo;
	}

	/**
	 * @return the createdBy
	 */
	public User getCreatedBy() {
		return createdBy;
	}

	/**
	 * @param createdBy
	 *            the createdBy to set
	 */
	public void setCreatedBy(final User createdBy) {
		this.createdBy = createdBy;
	}

	/**
	 * @return the priority
	 */
	public Priority getPriority() {
		return priority;
	}

	/**
	 * @param priority
	 *            the priority to set
	 */
	public void setPriority(final Priority priority) {
		this.priority = priority;
	}

	/**
	 * @return the story
	 */
	public Story getStory() {
		return story;
	}

	/**
	 * @param story
	 *            the story to set
	 */
	public void setStory(final Story story) {
		this.story = story;
	}

	/**
	 * @return the status
	 */
	public Status getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(final Status status) {
		this.status = status;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (prime * result) + ((name == null) ? 0 : name.hashCode());
		result = (prime * result) + ((story == null) ? 0 : story.hashCode());
		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Task other = (Task) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (story == null) {
			if (other.story != null) {
				return false;
			}
		} else if (!story.equals(other.story)) {
			return false;
		}
		return true;
	}

	@Override
	public int compareTo(final Task task) {
		return this.getName().compareTo(task.getName());
	}

}
